﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using TiPort.TiEntityObjects;
using MergeTI.TiEntityObjects.Clifar;
using System.IO;

namespace TI_Transport
{
    /// <summary>
    /// Загрузчик справочников связанных с Clifar и справочников организаций
    /// </summary>
    public class DictionaryLoader
    {

        public void  LoadINN(){
         XmlDocument d = new XmlDocument();
         d.Load(@"C:\Temp\Inn.xml");

         XmlSerializer mySerializer = new XmlSerializer(typeof(dataroot));
         XmlNodeReader r = new XmlNodeReader(d.ChildNodes[1]);
         var rr = (dataroot)mySerializer.Deserialize(r);
         foreach (Inn i in rr.List)
         {
             Helper.MongoTools.Instance.ExecuteScript("db.INN.insert(" + i.ToString() + ")");
         }
        }

        public void LoadOrganisation()
        {
            int j = 0;
            string[] fileList = Directory.GetFiles(@"c:\Temp\","*.xml");
            foreach (string s in fileList)
            {
                XmlDocument d = new XmlDocument();
                d.Load(s);
                
                d = Helper.XmlTools.GetXmlNoNamespaceDocument(d);

                XmlSerializer mySerializer = new XmlSerializer(typeof(masterDataBatch));
                XmlNodeReader r = new XmlNodeReader(d.ChildNodes[1]);
                var rr = (masterDataBatch)mySerializer.Deserialize(r);
                var c=rr.GetCollection();
                foreach (DictionaryOrganization  i in c )
                {
                    Helper.MongoTools.Instance.ExecuteScript("mergeOrganization(" + i.ToString() + ")");
                    ++j;
                    Console.WriteLine(j);
                }

                Helper.FileTools.RenameFile(s, s + "_OK"); 
 
            }

        }

    }
}
